<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>
        车辆添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/css/main.css}" href="${pageContext.request.contextPath }/statics/css/main.css" media="all">
    <script type="text/javascript" th:src="@{/lib/loading/okLoading.js}" src="${pageContext.request.contextPath }/statics/lib/loading/okLoading.js"></script>
    <style>
        .imgs {
            display: none;
        }
    </style>

</head>
<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>车辆管理</cite></a>
              <a><cite>车辆添加</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon"
                                                                        style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <form class="layui-form layui-form-pane" action="" id="add">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <input type="hidden" name="id" value="${param.id}">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>所属车主
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="owner" autocomplete="off" placeholder="请输入物主姓名"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>车牌号码
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="licensePlate" autocomplete="off" placeholder="请输入车牌号码"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            车位
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="parking" autocomplete="off" placeholder="请输入车辆颜色"
                                   class="layui-input" value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            车辆颜色
                        </label>
                        <div class="layui-input-block">
                            <input type="text" name="color" autocomplete="off" placeholder="请输入车辆颜色"
                                   class="layui-input" value="">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red">*</span>车辆缩略图
                        </label>
                        <div class="layui-input-inline">
                            <div class="site-demo-upbar">
                                <div class=" layui-upload-button" style="border:#FFFFFF ;">
                                    <button type="button" class="layui-btn" id="test1">
                                        <i class="layui-icon">&#xe67c;</i>上传图片
                                    </button>
                                    <input class="layui-upload" type="file" accept="undefined" id="previewImg"
                                           name="images" onchange="upload(this,)">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item imgs" id="imgshow">
                        <label class="layui-form-label">车辆缩略图展示
                        </label>
                        <img src="" id="pimages" name="pimages" style="width: 400px;height: 200px;"/>
                        <input id="avatar" name="photo" required="" type="hidden" value="">
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">
                            描述
                        </label>
                        <div class="layui-input-block">
                            <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item" align="center">
                        <button class="layui-btn" type="button" lay-filter="add" lay-submit="">
                            保存
                        </button>
                        <button class="layui-btn" type="reset">
                            重置
                        </button>
                    </div>
                </div>
                <div style="height:100px;"></div>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/lib/layui/layui.js}" src="${pageContext.request.contextPath }/statics/lib/layui/layui.js" charset="utf-8"></script>
<script th:src="@{/js/x-layui.js}" src="${pageContext.request.contextPath }/statics/js/x-layui.js" charset="utf-8"></script>
<script th:src="@{/js/jquery.min.js}" src="${pageContext.request.contextPath }/statics/js/jquery.min.js" charset="utf-8"></script>

<script>

    if ($("input[name='id']").val() != "") {    //根据id查询数据
        var id = $("input[name='id']").val()
        $.get("/vehicle/" + id, function (data) {
            console.log(data)
            $("input[name='photo']").val(data.data.photo)
            $("input[name='owner']").val(data.data.owner)
            $("input[name='color']").val(data.data.color)
            $("input[name='licensePlate']").val(data.data.licensePlate)
            $("input[name='dong']").val(data.data.dong)
            $("textarea[name='remark']").val(data.data.remark)
            if (data.data.photo != "" && data.data.photo != null) {
                $("#pimages").attr('src', data.data.photo);
                $("#avatar").val(data.data.photo);
                $(".imgs").show();
            }
        })
    }

    layui.use(['element', 'layer', 'form'], function () {
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        form = layui.form();
        okLoading.close($);
        form.render();
        //监听提交
        form.on('submit(add)', function (data) {
            var owner = $("input[name='owner']").val();
            var licensePlate = $("input[name='licensePlate']").val();
            var color = $("input[name='color']").val();

            if (owner == "") {
                layer.msg('所属车主不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (licensePlate == "") {
                layer.msg('车牌号不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (color == "") {
                layer.msg("车辆颜色不能为空！", {icon: 5, time: 2000});
                return false;
            }
            var data = data.field;
            if ($("input[name='id']").val() != "") {    //修改
                $.ajax({
                    type: "put",
                    url: "update",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        console.log(data)
                        if (data.data == true) {
                            layer.msg(data.msg, {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        }
                        else {
                            layer.msg("操作失败", {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }

            if ($("input[name='id']").val() == "") {    //增加
                $.ajax({
                    type: "post",
                    url: "add",
                    data: data,
                    dataType: "json",
                    success: function (data) {
                        if (data.data == true) {
                            layer.msg(data.msg, {icon: 6, time: 2000}, function () {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                            return false;
                        } else {
                            layer.msg("操作失败", {icon: 5, time: 2000});
                            return false;
                        }
                    }
                });
            }

        });
    })
</script>
<!--栏目缩略图上传-->
<script>
    function upload(obj, id) {
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        //console.log(formData);
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type: "post",
            processData: false,
            contentType: false,
            url: "/upload/uploadFile",
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.msg == '执行成功') {
                    layer.closeAll('loading');
                    //layer.msg(data.info,{icon:1,time:1000});
                    $("#pimages").attr('src', data.data);
                    $("#avatar").val(data.data);
                    $(".imgs").show();
                    return false;
                } else {
                    layer.msg(data.info, {icon: 2, time: 1000});
                }
            }
        });
    }
</script>

</body>
</html>